/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.service.impl;

import com.koron.common.bean.DepartmentTreeBean;
import com.koron.common.domain.DataBean;
import com.koron.common.web.Util;
import com.koron.report.bean.UserTaskAnalysis;
import com.koron.report.mapper.DataAnalysisMapper;
import com.koron.report.service.api.UserTaskAnalysisService;
import com.koron.report.util.BusinessCode;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.service.api.CorpManagementService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserTaskAnalysisServiceImpl implements UserTaskAnalysisService {

    private final static  Logger logger= LoggerFactory.getLogger(UserTaskAnalysisServiceImpl.class);

    @Autowired
    private CorpManagementService corpManagementService;

    @Autowired
    private Util util;
    @Override
    public MessageBean<DataBean<UserTaskAnalysis>> getUserTaskAnalysisListByLevel(Integer orgLevel, String orgCode, Date startTime, Date endTime, Integer page, Integer pageSize) {
        MessageBean<DataBean<UserTaskAnalysis>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            List<UserTaskAnalysis>analysisList=new ArrayList<>();
            switch (orgLevel){
                case 0:
                    //集团，返回所有水司数据
                    msg=getUserTaskAnalysisListByCorp(startTime,endTime,page,pageSize);
                    return msg;
                case 1:
                    //水司
                    analysisList=factory.getMapper(DataAnalysisMapper.class).queryCountUserTaskByGridList(startTime,endTime,orgCode);
                    for(UserTaskAnalysis taskAnalysis:analysisList){
                        //设置部门名称
                        if(StringUtils.isNotEmpty(taskAnalysis.getDepartmentCode())){
                            List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(taskAnalysis.getDepartmentCode(),0).getData();
                            if (null!=treeBeanList&&treeBeanList.size()>0)
                                taskAnalysis.setDepartmentName(treeBeanList.get(0).getName());
                        }else{
                            CorpBean corpBean1=corpManagementService.getOne(orgCode).getData();
                            taskAnalysis.setDepartmentName(corpBean1.getCorpName());
                        }
                    }
                    break;
                default:
                    break;
            }
            if(analysisList!=null&&analysisList.size()>0){
                Integer totalNumber=analysisList.size();
                pageSize=pageSize==null||pageSize<=0?totalNumber:pageSize;
                Integer start=0,end=15,totalPage=1;
                if(page!=null&&page>1){
                    if(pageSize==null||pageSize<=0){
                        pageSize=15;
                    }
                    start=(page-1)*pageSize;
                    end=start+pageSize;
                }
                if (end>totalNumber){
                    end=totalNumber;
                    if (start>end){
                        start=end;
                    }
                    totalPage = totalNumber / pageSize + 1;
                }
                DataBean<UserTaskAnalysis> dataBean=new DataBean<>();
                dataBean.setPageSize(pageSize);
                dataBean.setPageNo(page);
                dataBean.setTotalPage(totalPage);
                dataBean.setTotalNumber(totalNumber);
                dataBean.setList(analysisList.subList(start,end));
                msg.setData(dataBean);
            }else{
                msg.setData(null);
            }
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }

    }

    @Override
    public MessageBean<DataBean<UserTaskAnalysis>> getUserTaskAnalysisListByCorp(Date startTime, Date endTime, Integer page, Integer pageSize) {
        MessageBean<DataBean<UserTaskAnalysis>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            List<UserTaskAnalysis>analysisList=new ArrayList<>();
            //获取已开通的水司列表
            List<CorpBean> corpBeanList=corpManagementService.corpBeanList();
            if(null!=corpBeanList&&corpBeanList.size()>0){
                for(CorpBean corpBean:corpBeanList){
                    List<String> corpCodeList=new ArrayList<>();
                    corpCodeList.add(corpBean.getCorpCode());
                    UserTaskAnalysis analysis2=factory.getMapper(DataAnalysisMapper.class).queryCountUserTask(startTime,endTime,corpCodeList);
                    if (null==analysis2)
                        analysis2=new UserTaskAnalysis();
                    analysis2.setDepartmentName(corpBean.getCorpName());
                    analysis2.setDepartmentCode(corpBean.getCorpCode());
                    analysisList.add(analysis2);
                }
            }
            if(analysisList!=null&&analysisList.size()>0){
                Integer totalNumber=analysisList.size();
                pageSize=pageSize==null||pageSize<=0?totalNumber:pageSize;
                Integer start=0,end=15,totalPage=1;
                if(page!=null&&page>1){
                    if(pageSize==null||pageSize<=0){
                        pageSize=15;
                    }
                    start=(page-1)*pageSize;
                    end=start+pageSize;
                }
                if (end>totalNumber){
                    end=totalNumber;
                    if (start>end){
                        start=end;
                    }
                    totalPage = totalNumber / pageSize + 1;
                }
                DataBean<UserTaskAnalysis> dataBean=new DataBean<>();
                dataBean.setPageSize(pageSize);
                dataBean.setPageNo(page);
                dataBean.setTotalPage(totalPage);
                dataBean.setTotalNumber(totalNumber);
                dataBean.setList(analysisList.subList(start,end));
                msg.setData(dataBean);
            }else{
                msg.setData(null);
            }
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
}
